package com.company.test.sync;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhangyan
 * @version $ Id: Demo.java, v 0.1 2020/8/6 18:46 zhangyan Exp $
 */
public class RLDemo1 {
    Lock lock = new ReentrantLock();
    public void test1() {
        lock.lock();
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(i);
        }

    }

    public void test2() {
        System.out.println("test准备");
        lock.lock();
        System.out.println("test2()");
        lock.unlock();
    }

    public static void main(String[] args) {
        RLDemo1 demo = new RLDemo1();
        new Thread(demo::test1).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(demo::test2).start();
    }
}
